package com.client;

import java.util.ArrayList;
import java.util.List;

import com.client.controller.AnneeTextBoxController;
import com.client.controller.EntryController;
import com.client.controller.IfThenCheckController;
import com.client.controller.JourTextBoxController;
import com.client.controller.MoisTextBoxController;
import com.client.controller.NotNullTextBoxController;
import com.client.controller.NumericController;
import com.client.forms.AdresseEditor;
import com.client.service.UserManagementService;
import com.client.service.UserManagementServiceAsync;
import com.client.service.data.AssureDataService;
import com.client.service.data.AssureDataServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.shared.context.User;
import com.shared.model.Assure;
import com.shared.utilitaire.Adresse;
import com.shared.utilitaire.Date;

public class PanelModifProfil extends Composite {
	
	private TextBox boxNom = new TextBox();
	private TextBox boxPrenom = new TextBox();
	private TextBox boxJour = new TextBox();
	private TextBox boxMois = new TextBox();
	private TextBox boxAnnee = new TextBox();
	private TextBox boxJourPermis = new TextBox();
	private TextBox boxMoisPermis = new TextBox();
	private TextBox boxAnneePermis = new TextBox();
	private RadioButton radioOui = new RadioButton("new name", "Oui");
	private RadioButton radioNon = new RadioButton("new name", "Non");
	private TextBox boxRecupJour = new TextBox();
	private TextBox boxRecupMois = new TextBox();
	private TextBox boxRecupAnnee = new TextBox();
	private TextBox boxMail = new TextBox();
	private TextBox boxTel = new TextBox();
	private TextBox boxMobile = new TextBox();
	private ListBox comboCivilite = new ListBox();
	
	
	private List<EntryController> listEntryControllers = new ArrayList<EntryController>();
	
	private final UserManagementServiceAsync userService = GWT.create(UserManagementService.class);
	private final AssureDataServiceAsync assureService = GWT.create(AssureDataService.class);
	private Assure assure = new Assure() ;
	private boolean isRecup = false;
	
    IfThenCheckController jourRecupController = new IfThenCheckController(new JourTextBoxController(boxRecupJour, "Jour de reobtention"), isRecup);
    IfThenCheckController moisRecupController = new IfThenCheckController(new MoisTextBoxController(boxRecupMois, "Mois de reobtention"), isRecup);
    IfThenCheckController anneeRecupController = new IfThenCheckController(new AnneeTextBoxController(boxRecupAnnee, "Annee de reobtention"), isRecup);
	public PanelModifProfil(){
		listEntryControllers.add(jourRecupController);
    	listEntryControllers.add(moisRecupController);
    	listEntryControllers.add(anneeRecupController);
	}
	
	@SuppressWarnings("deprecation")
	public AbsolutePanel createModifProfil(){
		
		AbsolutePanel panelModifProfil = new AbsolutePanel();
		

		//Adresse
		final AbsolutePanel panelAdresse = new AbsolutePanel();
		panelModifProfil.add(panelAdresse, 6, 265);
		panelAdresse.setSize("412px", "400px");
		final PanelAdresse panAdr = new PanelAdresse("Adresse Principale:");
		final PanelAdresse panAdr2 = new PanelAdresse("Adresse Supplementaire :");
		
		final List<String> listeCivilite = new ArrayList<String>() ;
		listeCivilite.add("Mr");
		listeCivilite.add("Mme") ;
		listeCivilite.add("Melle");
		for(String s: listeCivilite){
			comboCivilite.addItem(s) ;
		}
		
		userService.getAssureCourant(new AsyncCallback<Assure>() {
			
			
			public void onSuccess(Assure result) {
				assure = result;
				comboCivilite.setSelectedIndex(listeCivilite.indexOf(assure.getCivilite()));
				boxNom.setValue(assure.getNom());
				boxPrenom.setValue(assure.getPrenom());
				boxJour.setValue(assure.getDateNaissance().getJourString());
				boxMois.setValue(assure.getDateNaissance().getMoisString());
				boxAnnee.setValue(String.valueOf(assure.getDateNaissance().getAnnee()));
				boxJourPermis.setValue(String.valueOf(assure.getDatePermis().getJourString()));
				boxMoisPermis.setValue(assure.getDatePermis().getMoisString());
				boxAnneePermis.setValue(String.valueOf(assure.getDatePermis().getAnnee()));
				radioOui.setValue(!assure.isPermisAnnule());
				radioNon.setValue(assure.isPermisAnnule());
				if(assure.isPermisAnnule()){
					boxRecupJour.setValue(assure.getDateReobtention().getJourString());
					boxRecupMois.setValue(assure.getDateReobtention().getMoisString());
					boxRecupAnnee.setValue(String.valueOf(assure.getDateReobtention().getAnnee()));
					isRecup = true ;
				}else{
					isRecup = false ;
					boxRecupJour.setEnabled(false);boxRecupMois.setEnabled(false);boxRecupAnnee.setEnabled(false);
				}
				panAdr.getBoxCodePostal().setValue(assure.getAdresse1().getCodePostal());
				panAdr.getBoxNumero().setValue(assure.getAdresse1().getNumero());
				panAdr.getBoxPays().setValue(assure.getAdresse1().getPays());
				panAdr.getBoxVille().setValue(assure.getAdresse1().getVille());
				panAdr.getBoxVoie().setValue(assure.getAdresse1().getNomVoie());
				
				if(assure.getAdresse2() != null){
					panAdr2.getBoxCodePostal().setValue(assure.getAdresse2().getCodePostal());
					panAdr2.getBoxNumero().setValue(assure.getAdresse2().getNumero());
					panAdr2.getBoxPays().setValue(assure.getAdresse2().getPays());
					panAdr2.getBoxVille().setValue(assure.getAdresse2().getVille());
					panAdr2.getBoxVoie().setValue(assure.getAdresse2().getNomVoie());
					
				}
				
				boxMail.setValue(assure.getMail());
				boxTel.setValue(assure.getNumeroTel());
				boxMobile.setValue(assure.getNumeroMobile());
			};
			
			
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
			
		});
		

		panelModifProfil.setSize("674px", "600px");
		
		Label lblInscription = new Label("Modifiez votre profil");
		lblInscription.setStyleName("titre") ;
		panelModifProfil.add(lblInscription, 6, 6);
		
		Label labelCivilite = new Label("Civilite :");
		labelCivilite.addStyleName("gras");
		panelModifProfil.add(labelCivilite, 6, 56);
		
		


		comboCivilite.setSelectedIndex(0);

		panelModifProfil.add(comboCivilite, 95, 50);
		
		Label labelNom = new Label("Nom :");
		labelNom.addStyleName("gras");
		panelModifProfil.add(labelNom, 6, 87);
		
		Label labelPrenom = new Label("Prenom :");
		labelPrenom.addStyleName("gras");
		panelModifProfil.add(labelPrenom, 6, 119);
		
		panelModifProfil.add(boxNom, 95, 86);
		boxNom.setWidth("179px");
		
		panelModifProfil.add(boxPrenom, 95, 119);
		boxPrenom.setWidth("179px");
		
		Label labelNaissance = new Label("Date de naissance :");
		labelNaissance.addStyleName("gras");
		panelModifProfil.add(labelNaissance, 6, 154);
		
		panelModifProfil.add(boxJour, 167, 154);
		boxJour.setWidth("27px");
		
		Label slash = new Label("/");
		panelModifProfil.add(slash, 200, 154);
		slash.setSize("14px", "21px");
		
		panelModifProfil.add(boxMois, 210, 154);
		boxMois.setWidth("27px");
		
		Label slash2 = new Label("/");
		panelModifProfil.add(slash2, 243, 154);
		slash2.setSize("14px", "21px");
		
		panelModifProfil.add(boxAnnee, 253, 154);
		boxAnnee.setWidth("47px");
		
		Label labelDatePermis = new Label("Date d'obtention du permis :");
		labelDatePermis.addStyleName("gras");
		panelModifProfil.add(labelDatePermis, 6, 181);
		
		panelModifProfil.add(boxJourPermis, 220, 182);
		boxJourPermis.setWidth("27px");
		
		Label slash3 = new Label("/");
		panelModifProfil.add(slash3, 253, 182);
		slash3.setSize("14px", "21px");
		
		panelModifProfil.add(boxMoisPermis, 263, 182);
		boxMoisPermis.setWidth("27px");
		
		Label slash4 = new Label("/");
		panelModifProfil.add(slash4, 296, 182);
		slash4.setSize("14px", "21px");
		
		panelModifProfil.add(boxAnneePermis, 306, 182);
		boxAnneePermis.setWidth("47px");
		
		Label labelValide = new Label("Est-il valide ?");
		labelValide.addStyleName("gras");
		panelModifProfil.add(labelValide, 6, 208);
		
		panelModifProfil.add(radioOui, 108, 208);
		
		panelModifProfil.add(radioNon, 167, 208);
		radioNon.setSize("44px", "21px");
		
		radioNon.addClickHandler(new ClickHandler() {
			
            
			
			public void onClick(ClickEvent event) {
				
				boxRecupAnnee.setEnabled(true);
            	boxRecupMois.setEnabled(true);
            	boxRecupJour.setEnabled(true);
            	isRecup=true;
            	jourRecupController.setCondition(true);
            	moisRecupController.setCondition(true);
            	anneeRecupController.setCondition(true);
			}
		});
        
        radioOui.addClickHandler(new ClickHandler() {
			
			
			public void onClick(ClickEvent event) {
				
				boxRecupAnnee.setEnabled(false);
            	boxRecupMois.setEnabled(false);
            	boxRecupJour.setEnabled(false);	
            	isRecup = false ;
            	jourRecupController.setCondition(false);
            	moisRecupController.setCondition(false);
            	anneeRecupController.setCondition(false);
			}
		});

		
		Label labelRecup = new Label("Si oui, quand le recuperez-vous ?");
		labelRecup.addStyleName("gras");
		panelModifProfil.add(labelRecup, 6, 232);
		
		
		panelModifProfil.add(boxRecupJour, 253, 230);
		boxRecupJour.setWidth("27px");
		
		Label slash5 = new Label("/");
		panelModifProfil.add(slash5, 286, 230);
		slash5.setSize("14px", "21px");
		
		panelModifProfil.add(boxRecupMois, 296, 230);
		boxRecupMois.setWidth("27px");
		
		Label slash6 = new Label("/");
		panelModifProfil.add(slash6, 329, 230);
		slash6.setSize("14px", "21px");
		
		panelModifProfil.add(boxRecupAnnee, 339, 230);
		boxRecupAnnee.setWidth("47px");
		
		panelAdresse.add(panAdr);

		panAdr.getComboBox().setSelectedIndex(0);

		
		final Hyperlink linkAdresse2 = new Hyperlink("Adresse Supplementaire", "Adresse2");
		final Hyperlink linkAdresse1 = new Hyperlink("Retour","Etape precedente");
		
		panelAdresse.add(linkAdresse2, 256, 261);
		linkAdresse2.setSize("150px", "21px");
		
		linkAdresse2.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	
	            panelAdresse.clear();
	            panelAdresse.add(panAdr2) ;	            
	        	
	        	panelAdresse.add(linkAdresse1, 256, 261);
	 	        }
	 	    });
		
		
		
		linkAdresse1.addClickHandler(new ClickHandler() {			
			
			public void onClick(ClickEvent event) {
				
				panelAdresse.clear();
	            
	            panelAdresse.add(panAdr) ;
	            
	        	
	        	
	        	panelAdresse.add(linkAdresse2, 256, 261);
				
			}
		});
		
		
		Label labelMail = new Label("Mail :");
		labelMail.addStyleName("gras");
		panelModifProfil.add(labelMail, 364, 56);
		
		panelModifProfil.add(boxMail, 406, 56);
		boxMail.setWidth("224px");
		
		Label labelTel = new Label("Telephone :");
		labelTel.addStyleName("gras");
		panelModifProfil.add(labelTel, 316, 99);
		
		panelModifProfil.add(boxTel, 406, 99);
		
		Label labelMobile = new Label("Mobile :");
		labelMobile.addStyleName("gras");
		panelModifProfil.add(labelMobile, 346, 136);
		
		panelModifProfil.add(boxMobile, 406, 135);
		
		Button boutonEnvoyer = new Button("New button");
        boutonEnvoyer.setText("Modifier");
        
        
        //Controllers
        listEntryControllers.add(new JourTextBoxController(boxJour,"NaissanceJour"));
        listEntryControllers.add(new JourTextBoxController(boxJourPermis,"PermisJour"));
        listEntryControllers.add(new MoisTextBoxController(boxMois, "NaissanceMois"));
        listEntryControllers.add(new MoisTextBoxController(boxMoisPermis, "PermisMois"));
        listEntryControllers.add(new AnneeTextBoxController(boxAnnee, "NaissanceAnnee"));
        listEntryControllers.add(new AnneeTextBoxController(boxAnneePermis, "PermisAnnee"));
        
        listEntryControllers.add(new NumericController(boxAnneePermis, "PermisAnnee"));
        listEntryControllers.add(new NumericController(boxMoisPermis, "PermisMois"));
        listEntryControllers.add(new NumericController(boxJourPermis, "PermisJour"));
        
        listEntryControllers.add(new NumericController(boxAnnee, "NaissanceAnnee"));
        listEntryControllers.add(new NumericController(boxMois, "NaissanceMois"));
        listEntryControllers.add(new NumericController(boxJour, "NaissanceJour"));
        
        listEntryControllers.add(new NotNullTextBoxController(boxNom, "Nom"));
        listEntryControllers.add(new NotNullTextBoxController(boxPrenom, "Prenom"));
        listEntryControllers.add(new NotNullTextBoxController(boxMail, "Mail"));

        
        boutonEnvoyer.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                		String errorMsg ="";
                		boolean correct = true;
                		for(EntryController entryController : listEntryControllers){
                			if(!entryController.check()){
                				correct=false;
                				errorMsg = errorMsg+"\n"+entryController.getSource();
                			}
                		}
                		
                		if(correct){
                
                        Adresse adr = new Adresse();
                        Adresse adrSup = new Adresse();

                        adr.setCodePostal(panAdr.getBoxCodePostal().getValue()) ;
                        adr.setNomVoie(panAdr.getBoxVoie().getValue()) ;
                        adr.setNumero(panAdr.getBoxNumero().getValue());
                        adr.setPays(panAdr.getBoxPays().getValue());
                        adr.setTypeVoie(panAdr.getComboBox().getValue(panAdr.getComboBox().getSelectedIndex()));
                        adr.setVille(panAdr.getBoxVille().getValue()) ;

                        adrSup.setCodePostal(panAdr2.getBoxCodePostal().getValue()) ;
                        adrSup.setNomVoie(panAdr2.getBoxVoie().getValue()) ;
                        adrSup.setNumero(panAdr2.getBoxNumero().getValue());
                        adrSup.setPays(panAdr2.getBoxPays().getValue());
                        adrSup.setTypeVoie(panAdr2.getComboBox().getValue(panAdr.getComboBox().getSelectedIndex()));
                        adrSup.setVille(panAdr2.getBoxVille().getValue()) ;
                        
                        //Assure ass1 = new Assure();
                        assure.setAdresse1(adr);
                        assure.setAdresse2(adrSup);
                        
                        assure.setCivilite(getComboCivilite().getValue(getComboCivilite().getSelectedIndex()));
                        assure.setDateNaissance(new Date(getBoxJour().getValue(),getBoxMois().getValue(),getBoxAnnee().getValue()));
                        assure.setDatePermis(new Date(getBoxJourPermis().getValue(),getBoxMoisPermis().getValue(),getBoxAnneePermis().getValue()));
                        
                        if (isRecup) {
                        	assure.setDateReobtention(new Date(getBoxRecupJour().getValue(),getBoxRecupMois().getValue(),getBoxRecupAnnee().getValue()));
                        }
                        
                        assure.setMail(getBoxMail().getValue());
                        assure.setNom(getBoxNom().getValue());
                        assure.setNumeroMobile(getBoxMobile().getValue());
                        assure.setNumeroTel(getBoxTel().getValue());
                        assure.setPrenom(getBoxPrenom().getValue());
                        assure.setPermisAnnule(isRecup);
                
               
                        assureService.update(assure, new AsyncCallback<Assure>() {


                                public void onSuccess(Assure result) {
                                        Window.confirm("Modification effectuées pour "+result.getNom());

                                }
                               
                               
                                public void onFailure(Throwable caught) {
                                        Window.alert(caught.getMessage());
                                       
                                }
                        });
                		}else{
                			Window.alert(errorMsg);
                		}
            	
                }

        });
        panelModifProfil.add(boutonEnvoyer, 500, 250);
		
		return panelModifProfil;
		
	}
	
	public TextBox getBoxNom() {
		return boxNom;
	}

	public TextBox getBoxPrenom() {
		return boxPrenom;
	}

	public TextBox getBoxJour() {
		return boxJour;
	}

	public TextBox getBoxMois() {
		return boxMois;
	}

	public TextBox getBoxAnnee() {
		return boxAnnee;
	}

	public TextBox getBoxJourPermis() {
		return boxJourPermis;
	}

	public TextBox getBoxMoisPermis() {
		return boxMoisPermis;
	}

	public TextBox getBoxAnneePermis() {
		return boxAnneePermis;
	}

	public RadioButton getRadioOui() {
		return radioOui;
	}

	public RadioButton getRadioNon() {
		return radioNon;
	}

	public TextBox getBoxRecupJour() {
		return boxRecupJour;
	}

	public TextBox getBoxRecupMois() {
		return boxRecupMois;
	}

	public TextBox getBoxRecupAnnee() {
		return boxRecupAnnee;
	}

	public TextBox getBoxMail() {
		return boxMail;
	}

	public TextBox getBoxTel() {
		return boxTel;
	}

	public TextBox getBoxMobile() {
		return boxMobile;
	}

	public ListBox getComboCivilite() {
		return comboCivilite;
	}

	public void enableControls(boolean etat){
		
		boxNom.setEnabled(etat);
    	boxPrenom.setEnabled(etat);
    	boxJour.setEnabled(etat);
    	boxMois.setEnabled(etat);
    	boxAnnee.setEnabled(etat);
    	boxJourPermis.setEnabled(etat);
    	boxMoisPermis.setEnabled(etat);
    	boxAnneePermis.setEnabled(etat);
    	radioOui.setEnabled(etat);
    	radioNon.setEnabled(etat);
    	boxRecupJour.setEnabled(etat);
    	boxRecupMois.setEnabled(etat);
    	boxRecupAnnee.setEnabled(etat);
    	boxMail.setEnabled(etat);
    	boxTel.setEnabled(etat);
    	boxMobile.setEnabled(etat);
    	comboCivilite.setEnabled(etat);
	}
	
}
